package com.item.controller;


import com.github.pagehelper.PageInfo;
import com.item.bean.RPage;
import com.item.domain.UserRole;
import com.item.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TUserRoleController {

    @Autowired
    UserRoleService userRoleService;

    @PostMapping(value = "/userRole/{limit}/{page}")
    public RPage<UserRole> UserRole(
            @PathVariable(value = "limit", required = false) int limit,
            @PathVariable(value = "page", required = false) int page,
            @RequestBody(required = false) UserRole userRole) {
        List<UserRole> userRoles = userRoleService.findAll(limit, page, userRole);
        PageInfo<UserRole> pageInfo = new PageInfo<>(userRoles);
        RPage<UserRole> rPage = new RPage<>();
        rPage.setList(pageInfo.getList());
        rPage.setPageNum(pageInfo.getPageNum());
        rPage.setTotal(pageInfo.getTotal());
        rPage.setPages(pageInfo.getPages());
        rPage.setPageSize(pageInfo.getPageSize());

        return rPage;
    }

    @PostMapping(value = "/userRole/add")
    public Integer userRoleAdd(@RequestBody UserRole userRole) {

        int i = userRoleService.add(userRole);
        return i;
    }

    @PutMapping(value = "/userRole/update")
    public Integer userRoleUpdate(@RequestBody UserRole userRole) {

        int i = userRoleService.update(userRole);
        return i;
    }

    @DeleteMapping(value = "/userRole/{id}")
    public Integer userRoleDelete(@PathVariable(value = "id") int id) {

        int i = userRoleService.delete(id);
        return i;
    }

}
